#!/usr/local/bin/python2.4
# -*- coding: Latin-1 -*-
"""

Copyright (c) 2007, Kjell Magne Fauske

"""

# Copyright (c) 2007, Kjell Magne Fauske
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

__author__ = 'Kjell Magne Fauske'
__version__ = 'dev'
__license__ = 'MIT'


from itertools import izip
from optparse import OptionParser
import optparse
import sys, tempfile, os, string,re
import logging
import tempfile
from StringIO import StringIO

# intitalize logging module
log = logging.getLogger("mdl2tex")
console = logging.StreamHandler()
console.setLevel(logging.WARNING)
# set a format which is simpler for console use
formatter = logging.Formatter('%(levelname)-8s %(message)s')
# tell the handler to use this format
console.setFormatter(formatter)
log.addHandler(console)


special_chars = ['$','\\','%','_','#','{',r'}','^','&']
special_chars_escape = [r'\$', r'$\backslash$',r'\%',r'\_',r'\#',
                        r'\{',r'\}',r'\^{}',r'\&']
charmap = dict(zip(special_chars,special_chars_escape))
def escapeTeXChars(string):
    r"""Escape the special LaTeX-chars %{}_^

    Examples:

    >>> escapeTeXChars('10%')
    '10 \\%'
    >>> escapeTeXChars('%{}_^\\$')
    '\\%\\{\\}\\_\\^{}$\\backslash$\\$'
    """
    return "".join([charmap.get(c,c) for c in string])


def process_cmd_line():
    """Set up and parse command line options"""
    usage = "Usage: %prog [options] <files>"
    parser = OptionParser(usage)
    parser.add_option('-V','--version', dest = 'printversion', action='store_true',
                  help="Print version information and exit", default=False),
    parser.add_option('--runtests',dest='runtests',
                  help="Run testes", action="store_true",default=False)
    (options, args) = parser.parse_args()
    return options,args,parser

def print_version_info():
    print "mdl2tex version % s" % __version__

def _runtests():
    import doctest
    doctest.testmod()

def main():
    options, args,parser = process_cmd_line()
    if True:
        log.warning('Running doc-tests')
        _runtests()
        sys.exit(0)
    if options.debug:
        # initalize log handler
        import platform
        hdlr = logging.FileHandler('mdl2tex.log')
        log.addHandler(hdlr)
        formatter = logging.Formatter('%(asctime)s %(name)s %(levelname)s %(message)s')
        hdlr.setFormatter(formatter)
        log.setLevel(logging.DEBUG)
        log.info('------- Start of run -------')
        log.info('mdl2tex called with: %s' % sys.argv)
    else:
        pass
    log.info('Program started in %s' % os.getcwd())
    if options.printversion:
        #print options.hest
        print_version_info()
        sys.exit(0)
    
        
    log.info('------- End of run -------')




if __name__ == '__main__':
    main()
    
    

